package com.awesome.network.interceptor;


import com.awesome.network.callback.OnProgressListener;
import com.awesome.network.response.DownloadResponseBody;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Author: JFangZ
 * Email:janezhang1901@163.com
 * Date: 2021/4/28 18:07
 * Description:Interceptor for listen download progress.
 */
public class DownloadInterceptor implements Interceptor {
    private final OnProgressListener mListener;
    private final long mStart;

    public DownloadInterceptor(long start, OnProgressListener listener) {
        mStart = start;
        mListener = listener;
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());
        ResponseBody responseBody = response.body();
        if (responseBody == null) {
            return response;
        }
        return response.newBuilder().body(new DownloadResponseBody(responseBody, mListener, mStart)).build();
    }
}